﻿using App.Chat.Hubs;
using App.Chat.Models;
using App.Chat.Models.MongoModels;
using Zws.MongoDb;

namespace App.Chat.Application.Implements
{
    public class MongoService
    {
        private readonly MongoDbClientFacade _mongoDbClient;
        private readonly IRemoteEventTrigger _remoteEventTrigger;
        public MongoService(MongoDbClientFacade mongoDbClient, IRemoteEventTrigger remoteEventTrigger)
        {
            _mongoDbClient = mongoDbClient;
            _remoteEventTrigger = remoteEventTrigger;
        }
        public async Task FriendAskAddTipIncAsync(string userId)
        {
            await _mongoDbClient.UpdateOneAsync<UnReadAudit>(x => userId.Equals(x.UserId),
                builder => builder.Inc(x => x.FriendAskAddTipCount, 1));
            await _remoteEventTrigger.FriendAddAskCountUpdateEventAsync(userId);
        }
        public async Task FriendAddSuccessTipInc(string userId)
        {
            await _mongoDbClient.UpdateOneAsync<UnReadAudit>(x => userId.Equals(x.UserId),
                builder => builder.Inc(x => x.FriendAddSuccessTipCount, 1));
            await _remoteEventTrigger.FriendAddSuccessCountUpdateEventAsync(userId);
        }
        public async Task ChangeUserRemarkAsync(string userId, string sessionId, string remark)
        {
            await _mongoDbClient.UpdateOneAsync<SessionUsers, string>(x => userId.Equals(x.UserId)
            && sessionId.Equals(x.SessionId), x => x.SessionName, remark);
            await _remoteEventTrigger.UpdateSessionListEventAsync(userId);
        }

        public async Task DeleteChatRecordAsync(string userId, string friendId)
        {
            //聊天记录软删除，避免mysql数据漏更新
            //await _mongoDbClient.UpdateOneAsync<ChatRecord, bool>(x => userId.Equals(x.)
            //&& friendId.Equals(x.SessionId), x => x, remark);
        }
    }
}
